<template>
  <div>
        <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: 'index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-table :data="userList" style="width: 100%">
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="role_name" label="执行人员"> </el-table-column>
      <el-table-column prop="mobile" label="手机号"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column label="权限状态">
        <template slot-scope="scope">
          <el-switch
            @change="changeSwitch(scope.row)"
            v-model="scope.row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="人员操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            size="mini"
            @click="compileUser(scope.row)"
          ></el-button>

          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            size="mini"
            @click="deleteUser(scope.row.id)"
          ></el-button>

          <el-dropdown style="margin-left: 10px">
            <el-button
              type="warning"
              icon="el-icon-star-off"
              circle
              size="mini"
            ></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-for="(item, key) in usersList"
                :key="key"
                @click.native="userValue(scope.row, item.roleName, key)"
              >
                {{ item.roleName }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange($event)"
      @current-change="handleCurrentChange"
      :page-sizes="[3, 4, 6, 7]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="修改人员" :visible.sync="compileUserVisible">
      <el-form :model="compileData">
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="compileData.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="email">
          <el-input v-model="compileData.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="mobile">
          <el-input v-model="compileData.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="compileUserVisible = false">取 消</el-button>
        <el-button type="primary" @click="compileUserS(compileData.id)"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog title="添加人员" :visible.sync="dialogFormVisible">
      <el-form :model="addUserData">
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="addUserData.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="addUserData.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="email">
          <el-input v-model="addUserData.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="mobile">
          <el-input v-model="addUserData.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </div>
      
    </el-dialog>
    <el-row class="addBut">
      <el-button type="primary" plain @click="dialogFormVisible = true"
        >添加人员</el-button
      >
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userList: [],
      total: 0,
      pageSize: 3,
      dialogFormVisible: false,
      compileUserVisible: false,
      addUserData: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      compileData: [],
      // 角色列表
      usersList: [],
      pagenum: 1,
    };
  },

  methods: {
    // 修改角色信息
    userValue(a, b, k) {
      // 获取a 是 用户id b是角色id k 是下标
 
      a.role_name = b;
      this.$Req
        .put(`users/${a.id}/role`, { rid: this.usersList[k].id })
        .then((res) => {
          const { data, meta } = res.data;
          if (meta.status === 200) {
            this.$message({
              showClose: true,
              message: meta.msg,
              type: "success",
            });
          } else {
            this.$message.error(meta.msg);
          }
        });
    },
    // 设置角色列表
    async roleUser(role) {},
    // 删除用户
    deleteUser(uid) {
      this.$confirm("此操作将永久删除此人员, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.$Req.delete(`/users/${uid}`).then((res) => {
     

            const { data, meta } = res.data;
            if (meta.status === 200) {
              this.$message({
                showClose: true,
                message: meta.msg,
                type: "success",
              });
              this.getusers(this.pagenum);
            } else {
              this.$message.error(meta.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 修改用户
    compileUser(rowS) {

      this.compileData = rowS;
      this.compileUserVisible = true;
    },
    //确定修改用户方法
    async compileUserS() {

      await this.$Req
        .put(`/users/` + this.compileData.id, this.compileData)
        .then((res) => {
    
          const { data, meta } = res.data;
          this.compileUserVisible = false;

          if (meta.status === 200) {
            this.$message({
              // showClose: true,
              message: meta.msg,
              type: "success",
            });
          } else {
            this.$message.error(meta.msg);
          }
        });
    },
    // 修改用户状态
    async changeSwitch(rows) {
      // 发送状态修改请求
      await this.$Req
        .put(`users/${rows.id}/state/${rows.mg_state}`)
        .then((res) => {
          const { data, meta } = res.data;
    
          if (meta.status === 200) {
            this.$message({
              showClose: true,
              message: meta.msg,
              type: "success",
            });
          } else {
            this.$message.error(meta.msg);
          }
        });
    },

    // 获取用户数据列表
    async getusers(pagenum = 1, pagesize = 3) {
      var { data } = await this.$Req.get(
        `/users?pagenum=${pagenum}&pagesize=${pagesize}`
      );
      // 解决当前页面删除刷新
      this.pagenum = pagenum;
      if (data.meta.status == 200) {
        this.userList = data.data.users;
        this.total = data.data.total;
      } else {
        this.$message.error(data.meta.msg);
      }
    },
    handleSizeChange(pageSizeS) {
      this.pageSize = pageSizeS;
      this.getusers(1, pageSizeS);
    },
    handleCurrentChange(pagenum) {
      this.getusers(pagenum, this.pageSize);
    },
    async addUser() {
      this.$Req.post("/users", this.addUserData).then((res) => {
        const { data, meta } = res.data;
        if (meta.status == 201) {
          this.$message({
            showClose: true,
            message: meta.msg,
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: meta.msg,
            type: "error",
          });
        }
        this.userList = data;
        this.getusers(1, 3);
        // 待修改
        this.addUserData.username = "";
        this.addUserData.password = "";
        this.addUserData.email = "";
        this.addUserData.mobile = "";
      });
      this.dialogFormVisible = false;
    },
  },
  async mounted() {
    this.getusers(1, 3);

    // 获取角色列表
    this.$Req.get("/roles").then((res) => {

      this.usersList = res.data.data;
    });
  },
};
</script>

<style>
.addBut {
  float: right;
}
.el-main {
  line-height: 0px;
}
.el-table thead {
  font-size: 20px;
  color: rgb(19, 16, 16);
}
.el-main{
  line-height: 0;
}
</style>